  
# Load packages
  library(haven)
  library(broom)
  library(lmtest)
  library(sandwich)
  library(stargazer)
  library(tidyverse)

# Calculate robust confidence intervals
  se_robust <- function(x)
    coeftest(x, vcov = vcovHC(x, type = "HC0"))[, "Std. Error"]
  p_robust <- function(x)
    coeftest(x, vcov = vcovHC(x, type = "HC0"))[, "Pr(>|t|)"]
  
  ####### GLES Panel 2016-2021, Wellen 1-15

  # Load data from waves 3, 5, 6 and 7 (all 2017) as they contain the necessary coalition questions
  data <- read_dta("ObsStudy_00_data_GLES_panel_2016_2021_waves_2017.dta")
  data[data < 0] <- NA
  
  dt <- data %>% select(contains("kpx_"))
  dt <- dt[, c(2,5)] # gender and birthyear
  edu <- data %>% select(contains("kp1_2320")) # Schulabschluss, only asked in sample A1 and A2 of wave 1
  dt <- cbind(dt, edu)
  
  # Generate a gender dummy "male"
  dt$male <- ifelse(dt$kpx_2280 == 2, 0, dt$kpx_2280)

  # Age
  dt$kpx_2290s <- as.numeric(gsub("\\D", "", dt$kpx_2290s)) # transform to numeric
  dt$age <- 2017 - dt$kpx_2290s
  dt <- dt[, c(3:5)]
  
  #### wave 3
  dt3 <- data %>% select(contains("kp3_"))
  dt3 <- dt3[,c(1, 11, 29:35, 142:155)]
  
  # Evaluation political parties
  colnames(dt3)[seq(3, 9, 1)] <- c("rat_CDU", "rat_CSU", "rat_SPD", "rat_FDP", "rat_GRUENE", "rat_LINKE", "rat_AfD")
  
  # Coalition evaluation
  colnames(dt3)[seq(10, 16, 1)] <- c("rat_coal_CDU_CSU_SPD", "rat_coal_CDU_CSU_FDP", "rat_coal_SPD_GRUENE", "rat_coal_CDU_CSU_GRUENE", "rat_coal_SPD_FDP_GRUENE",  "rat_coal_CDU_CSU_FDP_GRUENE", "rat_coal_SPD_GRUENE_LINKE")
  
  # Coalition likelihood
  colnames(dt3)[seq(17, 23, 1)] <- c("coallik_CDU_CSU_SPD", "coallik_CDU_CSU_FDP", "coallik_SPD_GRUENE", "coallik_CDU_CSU_GRUENE", "coallik_SPD_FDP_GRUENE", "coallik_CDU_CSU_FDP_GRUENE", "coallik_SPD_GRUENE_LINKE")

  # Dependent variable
  colnames(dt3)[2] <- "vote"
  dt3$vote <- as.numeric(dt3$vote)
  
  # Assign interpretable labels to vote variable
  dt3$vote[dt3$vote==1] <- "UNION"
  dt3$vote[dt3$vote==4] <- "SPD"
  dt3$vote[dt3$vote==5] <- "FDP"
  dt3$vote[dt3$vote==6] <- "GRUENE"
  dt3$vote[dt3$vote==7] <- "LINKE"
  dt3$vote[dt3$vote==322] <- "AfD"
  
  # Generate ptv columns based on vote variable
  dt3$ptv_UNION <- ifelse(dt3$vote == "UNION", 1, 0)
  dt3$ptv_SPD <- ifelse(dt3$vote == "SPD", 1, 0)
  dt3$ptv_FDP <- ifelse(dt3$vote == "FDP", 1, 0)
  dt3$ptv_GRUENE <- ifelse(dt3$vote == "GRUENE", 1, 0)
  dt3$ptv_LINKE <- ifelse(dt3$vote == "LINKE", 1, 0)
  dt3$ptv_AfD <- ifelse(dt3$vote == "AfD", 1, 0)
  
  Z <- dt3 %>% select("rat_coal_CDU_CSU_SPD", "rat_coal_CDU_CSU_FDP", "rat_coal_SPD_GRUENE" , "rat_coal_CDU_CSU_GRUENE", "rat_coal_SPD_FDP_GRUENE", "rat_coal_CDU_CSU_FDP_GRUENE", "rat_coal_SPD_GRUENE_LINKE") %>%
    mutate_all(as.numeric) %>%
    mutate_all(funs(scales::rescale(.,to = c(0, 1)))) 
  
  ##### CDU/CSU #####
  # Coalition evaluation
  rat_coal_CDU_CSU <- Z %>% select(-c(rat_coal_SPD_GRUENE, rat_coal_SPD_FDP_GRUENE, rat_coal_SPD_GRUENE_LINKE)) %>%
    mutate_all(as.numeric) %>%
    as.matrix()
  
  # Coalition likelihood
  coal_lik_CDU_CSU <- dt3 %>% select(coallik_CDU_CSU_SPD, coallik_CDU_CSU_FDP, coallik_CDU_CSU_FDP_GRUENE, coallik_CDU_CSU_GRUENE)  %>%
    mutate_all(as.numeric) %>%
    mutate(sum_exp = exp(coallik_CDU_CSU_SPD) + exp(coallik_CDU_CSU_FDP) + exp(coallik_CDU_CSU_FDP_GRUENE) + exp(coallik_CDU_CSU_GRUENE),
           coallik_CDU_CSU_SPD = exp(coallik_CDU_CSU_SPD)/sum_exp,
           coallik_CDU_CSU_FDP = exp(coallik_CDU_CSU_FDP)/sum_exp,
           coallik_CDU_CSU_FDP_GRUENE = exp(coallik_CDU_CSU_FDP_GRUENE)/sum_exp,
           coallik_CDU_CSU_GRUENE = exp(coallik_CDU_CSU_GRUENE)/sum_exp) %>%
    select(-sum_exp) %>%
    as.matrix()
  
  ##### SPD #####
  # Coalition evaluation
  rat_coal_SPD <- Z %>% select(-c(rat_coal_CDU_CSU_FDP, rat_coal_CDU_CSU_GRUENE, rat_coal_CDU_CSU_FDP_GRUENE)) %>%
    mutate_all(as.numeric) %>%
    as.matrix()
  
  # Coalition likelihood
  coal_lik_SPD <- dt3 %>% select(coallik_CDU_CSU_SPD, coallik_SPD_GRUENE, coallik_SPD_FDP_GRUENE, coallik_SPD_GRUENE_LINKE)  %>%
    mutate_all(as.numeric) %>%
    mutate(sum_exp = exp(coallik_CDU_CSU_SPD) + exp(coallik_SPD_GRUENE) + exp(coallik_SPD_FDP_GRUENE) + exp(coallik_SPD_GRUENE_LINKE),
           coallik_CDU_CSU_SPD = exp(coallik_CDU_CSU_SPD)/sum_exp,
           coallik_SPD_GRUENE = exp(coallik_SPD_GRUENE)/sum_exp,
           coallik_SPD_FDP_GRUENE = exp(coallik_SPD_FDP_GRUENE)/sum_exp,
           coallik_SPD_GRUENE_LINKE = exp(coallik_SPD_GRUENE_LINKE)/sum_exp) %>%
    select(-sum_exp) %>%
    as.matrix()
  
  ##### FDP #####
  # Coalition evaluation
  rat_coal_FDP <- Z %>% select(-c(rat_coal_CDU_CSU_SPD, rat_coal_SPD_GRUENE, rat_coal_CDU_CSU_GRUENE, rat_coal_SPD_GRUENE_LINKE)) %>%
    mutate_all(as.numeric) %>%
    as.matrix()
  
  # Coalition likelihood
  coal_lik_FDP <- dt3 %>% select(coallik_CDU_CSU_FDP, coallik_SPD_FDP_GRUENE, coallik_CDU_CSU_FDP_GRUENE)  %>%
    mutate_all(as.numeric) %>%
    mutate(sum_exp = exp(coallik_CDU_CSU_FDP) + exp(coallik_SPD_FDP_GRUENE) + exp(coallik_CDU_CSU_FDP_GRUENE),
           coallik_CDU_CSU_FDP = exp(coallik_CDU_CSU_FDP)/sum_exp,
           coallik_SPD_FDP_GRUENE = exp(coallik_SPD_FDP_GRUENE)/sum_exp,
           coallik_CDU_CSU_FDP_GRUENE = exp(coallik_CDU_CSU_FDP_GRUENE)/sum_exp) %>%
    select(-sum_exp) %>%
    as.matrix()
  
  ##### GRUENE #####
  # Coalition evaluation
  rat_coal_GRUENE <- Z %>% select(-c(rat_coal_CDU_CSU_SPD, rat_coal_CDU_CSU_FDP)) %>%
    mutate_all(as.numeric) %>%
    as.matrix()
  
  # Coalition likelihood
  coal_lik_GRUENE <- dt3 %>% select(coallik_CDU_CSU_FDP_GRUENE, coallik_SPD_GRUENE, coallik_SPD_FDP_GRUENE, coallik_CDU_CSU_GRUENE, coallik_SPD_GRUENE_LINKE)  %>%
    mutate_all(as.numeric) %>%
    mutate(sum_exp =  exp(coallik_CDU_CSU_FDP_GRUENE) + exp(coallik_SPD_GRUENE) + exp(coallik_SPD_FDP_GRUENE) + exp(coallik_CDU_CSU_GRUENE) + exp(coallik_SPD_GRUENE_LINKE),
           coallik_CDU_CSU_FDP_GRUENE = exp(coallik_CDU_CSU_FDP_GRUENE)/sum_exp,
           coallik_SPD_GRUENE = exp(coallik_SPD_GRUENE)/sum_exp, 
           coallik_SPD_FDP_GRUENE = exp(coallik_SPD_FDP_GRUENE)/sum_exp,
           coallik_CDU_CSU_GRUENE = exp(coallik_CDU_CSU_GRUENE)/sum_exp,
           coallik_SPD_GRUENE_LINKE = exp(coallik_SPD_GRUENE_LINKE)/sum_exp) %>%
    select(-sum_exp) %>%
    as.matrix()
  
  ##### LINKE #####
  # Coalition evaluation
  rat_coal_LINKE <- Z %>% select(rat_coal_SPD_GRUENE_LINKE) %>%
    mutate_all(as.numeric) %>%
    as.matrix()
  
  # Coalition likelihood, has to be 1 as only one coalition option available
  coal_lik_LINKE <- dt3 %>% select(coallik_SPD_GRUENE_LINKE)  %>%
    mutate_all(as.numeric) %>%
    mutate(sum_exp =  exp(coallik_SPD_GRUENE_LINKE),
           coallik_SPD_GRUENE_LINKE = exp(coallik_SPD_GRUENE_LINKE)/sum_exp) %>%
    select(-sum_exp) %>%
    as.matrix()
  
  # No AfD Coalition Evaluation item included
  
  # Mean-variance model
  EV <- function(gamma,Z){
    gamma %*% Z
  }
  
  Vari <- function(gamma,Z){
    gamma %*% ((Z - as.numeric(EV(gamma,Z)))^2)
  }
  
  N <- nrow(coal_lik_CDU_CSU)
  M_CDU_CSU <- sapply(1:N, function(i) EV(coal_lik_CDU_CSU[i,], rat_coal_CDU_CSU[i,]))
  V_CDU_CSU <- sapply(1:N, function(i) Vari(coal_lik_CDU_CSU[i,], rat_coal_CDU_CSU[i,]))
  
  N <- nrow(coal_lik_SPD)
  M_SPD <- sapply(1:N, function(i) EV(coal_lik_SPD[i,], rat_coal_SPD[i,]))
  V_SPD <- sapply(1:N, function(i) Vari(coal_lik_SPD[i,], rat_coal_SPD[i,]))
  
  N <- nrow(coal_lik_FDP)
  M_FDP <- sapply(1:N, function(i) EV(coal_lik_FDP[i,], rat_coal_FDP[i,]))
  V_FDP <- sapply(1:N, function(i) Vari(coal_lik_FDP[i,], rat_coal_FDP[i,]))
  
  N <- nrow(coal_lik_GRUENE)
  M_GRUENE <- sapply(1:N, function(i) EV(coal_lik_GRUENE[i,], rat_coal_GRUENE[i,]))
  V_GRUENE <- sapply(1:N, function(i) Vari(coal_lik_GRUENE[i,], rat_coal_GRUENE[i,]))
  
  N <- nrow(coal_lik_LINKE)
  M_LINKE <- sapply(1:N, function(i) EV(coal_lik_LINKE[i,], rat_coal_LINKE[i,]))
  V_LINKE <- sapply(1:N, function(i) Vari(coal_lik_LINKE[i,], rat_coal_LINKE[i,])) # is 0
  
  # Add sociodemographic information
  dt3 <- cbind(dt3, dt)
  
  d3 <- data.frame(
    "ptv" = dt3$vote,
    "ptv_UNION" = dt3$ptv_UNION,
    "ptv_SPD" = dt3$ptv_SPD,
    "ptv_FDP" = dt3$ptv_FDP,
    "ptv_GRUENE" = dt3$ptv_GRUENE,
    "ptv_LINKE" = dt3$ptv_LINKE,
    "rat.UNION" = (dt3$rat_CDU + dt3$rat_CSU)/2,
    "rat.SPD" = dt3$rat_SPD,
    "rat.FDP" = dt3$rat_FDP,
    "rat.GRUENE" = dt3$rat_GRUENE,
    "rat.LINKE" = dt3$rat_LINKE,
    "lotterymean.UNION" = M_CDU_CSU,
    "lotteryvariance.UNION" = V_CDU_CSU,
    "lotterymean.SPD" = M_SPD,
    "lotteryvariance.SPD" = V_SPD,
    "lotterymean.FDP" = M_FDP,
    "lotteryvariance.FDP" = V_FDP,
    "lotterymean.GRUENE" = M_GRUENE,
    "lotteryvariance.GRUENE" = V_GRUENE,
    "lotterymean.LINKE" = M_LINKE,
    "lotteryvariance.LINKE" = V_LINKE,
    "sex" = dt3$male,
    "age" = dt3$age,
    "edu" = dt3$kp1_2320
  )
  
  d3$lotteryvariance.UNION <- scales::rescale(d3$lotteryvariance.UNION, to = c(0, 1), from = c(0,0.25))
  d3$lotteryvariance.SPD <- scales::rescale(d3$lotteryvariance.SPD, to = c(0, 1), from = c(0,0.25))
  d3$lotteryvariance.FDP <- scales::rescale(d3$lotteryvariance.FDP, to = c(0, 1), from = c(0,0.25))
  d3$lotteryvariance.GRUENE <- scales::rescale(d3$lotteryvariance.GRUENE, to = c(0, 1), from = c(0,0.25))
  d3$lotteryvariance.LINKE <- scales::rescale(d3$lotteryvariance.LINKE, to = c(0, 1), from = c(0,0.25))
  
# Save model results
  summary(m3a <- lm(ptv_UNION ~ lotteryvariance.UNION + lotterymean.UNION + rat.UNION  + sex + as.factor(edu) + age, d3))
  summary(m3b <- lm(ptv_SPD ~ lotteryvariance.SPD + lotterymean.SPD + rat.SPD + sex + as.factor(edu) + age, d3))
  summary(m3c <- lm(ptv_FDP ~ lotteryvariance.FDP + lotterymean.FDP + rat.FDP + sex + as.factor(edu) + age, d3))
  summary(m3d <- lm(ptv_GRUENE ~ lotteryvariance.GRUENE + lotterymean.GRUENE + rat.GRUENE + sex + as.factor(edu) + age, d3))
  summary(m3e <- lm(ptv_LINKE ~ lotteryvariance.LINKE + lotterymean.LINKE + rat.LINKE + sex + as.factor(edu) + age, d3))
  
  GER_2017_GLES_Panel_w3_UNION_Variance_Estimate <- tidy(m3a) %>% filter(term=="lotteryvariance.UNION") %>% select("estimate")
  GER_2017_GLES_Panel_w3_UNION_Variance_SE <- tidy(m3a) %>% filter(term=="lotteryvariance.UNION") %>% select("std.error")
  GER_2017_GLES_Panel_w3_UNION_Mean_Estimate <- tidy(m3a) %>% filter(term=="lotterymean.UNION") %>% select("estimate")
  GER_2017_GLES_Panel_w3_UNION_Mean_SE <- tidy(m3a) %>% filter(term=="lotterymean.UNION") %>% select("std.error")
  
  GER_2017_GLES_Panel_w3_SPD_Variance_Estimate <- tidy(m3b) %>% filter(term=="lotteryvariance.SPD") %>% select("estimate")
  GER_2017_GLES_Panel_w3_SPD_Variance_SE <- tidy(m3b) %>% filter(term=="lotteryvariance.SPD") %>% select("std.error")
  GER_2017_GLES_Panel_w3_SPD_Mean_Estimate <- tidy(m3b) %>% filter(term=="lotterymean.SPD") %>% select("estimate")
  GER_2017_GLES_Panel_w3_SPD_Mean_SE <- tidy(m3b) %>% filter(term=="lotterymean.SPD") %>% select("std.error")
  
  GER_2017_GLES_Panel_w3_FDP_Variance_Estimate <- tidy(m3c) %>% filter(term=="lotteryvariance.FDP") %>% select("estimate")
  GER_2017_GLES_Panel_w3_FDP_Variance_SE <- tidy(m3c) %>% filter(term=="lotteryvariance.FDP") %>% select("std.error")
  GER_2017_GLES_Panel_w3_FDP_Mean_Estimate <- tidy(m3c) %>% filter(term=="lotterymean.FDP") %>% select("estimate")
  GER_2017_GLES_Panel_w3_FDP_Mean_SE <- tidy(m3c) %>% filter(term=="lotterymean.FDP") %>% select("std.error")
  
  GER_2017_GLES_Panel_w3_GR_Variance_Estimate <- tidy(m3d) %>% filter(term=="lotteryvariance.GRUENE") %>% select("estimate")
  GER_2017_GLES_Panel_w3_GR_Variance_SE <- tidy(m3d) %>% filter(term=="lotteryvariance.GRUENE") %>% select("std.error")
  GER_2017_GLES_Panel_w3_GR_Mean_Estimate <- tidy(m3d) %>% filter(term=="lotterymean.GRUENE") %>% select("estimate")
  GER_2017_GLES_Panel_w3_GR_Mean_SE <- tidy(m3d) %>% filter(term=="lotterymean.GRUENE") %>% select("std.error")
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  #### wave 5
  dt5 <- data %>% select(contains("kp5_"))
  
  dt5 <- dt5[,c(1, 7, 11, 66:72, 112:125)]
  
  # Evaluation political parties
  colnames(dt5)[seq(4, 10, 1)] <- c("rat_CDU", "rat_CSU", "rat_SPD", "rat_FDP", "rat_GRUENE", "rat_LINKE", "rat_AfD")
  
  # Coalition evaluation
  colnames(dt5)[seq(11, 17, 1)] <- c("rat_coal_CDU_CSU_SPD", "rat_coal_CDU_CSU_FDP", "rat_coal_SPD_GRUENE", "rat_coal_CDU_CSU_GRUENE", "rat_coal_SPD_FDP_GRUENE",  "rat_coal_CDU_CSU_FDP_GRUENE", "rat_coal_SPD_GRUENE_LINKE")
  
  # Coalition likelihood
  colnames(dt5)[seq(18, 24, 1)] <- c("coallik_CDU_CSU_SPD", "coallik_CDU_CSU_FDP", "coallik_SPD_GRUENE", "coallik_CDU_CSU_GRUENE", "coallik_SPD_FDP_GRUENE", "coallik_CDU_CSU_FDP_GRUENE", "coallik_SPD_GRUENE_LINKE")
  
  # Generate party choice variable
  dt5$vote <- dt5$kp5_190bb
  dt5$vote <- ifelse(is.na(dt5$vote), dt5$kp5_191bb, dt5$vote)

  # Assign interpretable labels to vote variable
  dt5$vote[dt5$vote==1] <- "UNION"
  dt5$vote[dt5$vote==4] <- "SPD"
  dt5$vote[dt5$vote==5] <- "FDP"
  dt5$vote[dt5$vote==6] <- "GRUENE"
  dt5$vote[dt5$vote==7] <- "LINKE"
  dt5$vote[dt5$vote==322] <- "AfD"
  
  # Generate ptv columns based on vote variable
  dt5$ptv_UNION <- ifelse(dt5$vote == "UNION", 1, 0)
  dt5$ptv_SPD <- ifelse(dt5$vote == "SPD", 1, 0)
  dt5$ptv_FDP <- ifelse(dt5$vote == "FDP", 1, 0)
  dt5$ptv_GRUENE <- ifelse(dt5$vote == "GRUENE", 1, 0)
  dt5$ptv_LINKE <- ifelse(dt5$vote == "LINKE", 1, 0)
  dt5$ptv_AfD <- ifelse(dt5$vote == "AfD", 1, 0)
  
  Z <- dt5 %>% select("rat_coal_CDU_CSU_SPD", "rat_coal_CDU_CSU_FDP", "rat_coal_SPD_GRUENE" , "rat_coal_CDU_CSU_GRUENE", "rat_coal_SPD_FDP_GRUENE", "rat_coal_CDU_CSU_FDP_GRUENE", "rat_coal_SPD_GRUENE_LINKE") %>%
    mutate_all(as.numeric) %>%
    mutate_all(funs(scales::rescale(.,to = c(0, 1)))) 
  
  ##### CDU/CSU #####
  # Coalition evaluation
  rat_coal_CDU_CSU <- Z %>% select(-c(rat_coal_SPD_GRUENE, rat_coal_SPD_FDP_GRUENE, rat_coal_SPD_GRUENE_LINKE)) %>%
    mutate_all(as.numeric) %>%
    as.matrix()
  
  # Coalition Likelihood
  coal_lik_CDU_CSU <- dt5 %>% select(coallik_CDU_CSU_SPD, coallik_CDU_CSU_FDP, coallik_CDU_CSU_FDP_GRUENE, coallik_CDU_CSU_GRUENE)  %>%
    mutate_all(as.numeric) %>%
    mutate(sum_exp = exp(coallik_CDU_CSU_SPD) + exp(coallik_CDU_CSU_FDP) + exp(coallik_CDU_CSU_FDP_GRUENE) + exp(coallik_CDU_CSU_GRUENE),
           coallik_CDU_CSU_SPD = exp(coallik_CDU_CSU_SPD)/sum_exp,
           coallik_CDU_CSU_FDP = exp(coallik_CDU_CSU_FDP)/sum_exp,
           coallik_CDU_CSU_FDP_GRUENE = exp(coallik_CDU_CSU_FDP_GRUENE)/sum_exp,
           coallik_CDU_CSU_GRUENE = exp(coallik_CDU_CSU_GRUENE)/sum_exp) %>%
    select(-sum_exp) %>%
    as.matrix()
  
  ##### SPD #####
  # Coalition evaluation
  rat_coal_SPD <- Z %>% select(-c(rat_coal_CDU_CSU_FDP, rat_coal_CDU_CSU_GRUENE, rat_coal_CDU_CSU_FDP_GRUENE)) %>%
    mutate_all(as.numeric) %>%
    as.matrix()
  
  # Coalition likelihood
  coal_lik_SPD <- dt5 %>% select(coallik_CDU_CSU_SPD, coallik_SPD_GRUENE, coallik_SPD_FDP_GRUENE, coallik_SPD_GRUENE_LINKE)  %>%
    mutate_all(as.numeric) %>%
    mutate(sum_exp = exp(coallik_CDU_CSU_SPD) + exp(coallik_SPD_GRUENE) + exp(coallik_SPD_FDP_GRUENE) + exp(coallik_SPD_GRUENE_LINKE),
           coallik_CDU_CSU_SPD = exp(coallik_CDU_CSU_SPD)/sum_exp,
           coallik_SPD_GRUENE = exp(coallik_SPD_GRUENE)/sum_exp,
           coallik_SPD_FDP_GRUENE = exp(coallik_SPD_FDP_GRUENE)/sum_exp,
           coallik_SPD_GRUENE_LINKE = exp(coallik_SPD_GRUENE_LINKE)/sum_exp) %>%
    select(-sum_exp) %>%
    as.matrix()
  
  ##### FDP #####
  # Coalition evaluation
  rat_coal_FDP <- Z %>% select(-c(rat_coal_CDU_CSU_SPD, rat_coal_SPD_GRUENE, rat_coal_CDU_CSU_GRUENE, rat_coal_SPD_GRUENE_LINKE)) %>%
    mutate_all(as.numeric) %>%
    as.matrix()
  
  # Coalition likelihood
  coal_lik_FDP <- dt5 %>% select(coallik_CDU_CSU_FDP, coallik_SPD_FDP_GRUENE, coallik_CDU_CSU_FDP_GRUENE)  %>%
    mutate_all(as.numeric) %>%
    mutate(sum_exp = exp(coallik_CDU_CSU_FDP) + exp(coallik_SPD_FDP_GRUENE) + exp(coallik_CDU_CSU_FDP_GRUENE),
           coallik_CDU_CSU_FDP = exp(coallik_CDU_CSU_FDP)/sum_exp,
           coallik_SPD_FDP_GRUENE = exp(coallik_SPD_FDP_GRUENE)/sum_exp,
           coallik_CDU_CSU_FDP_GRUENE = exp(coallik_CDU_CSU_FDP_GRUENE)/sum_exp) %>%
    select(-sum_exp) %>%
    as.matrix()
  
  ##### GRUENE #####
  # Coalition evaluation
  rat_coal_GRUENE <- Z %>% select(-c(rat_coal_CDU_CSU_SPD, rat_coal_CDU_CSU_FDP)) %>%
    mutate_all(as.numeric) %>%
    as.matrix()
  
  # Coalition likelihood
  coal_lik_GRUENE <- dt5 %>% select(coallik_CDU_CSU_FDP_GRUENE, coallik_SPD_GRUENE, coallik_SPD_FDP_GRUENE, coallik_CDU_CSU_GRUENE, coallik_SPD_GRUENE_LINKE)  %>%
    mutate_all(as.numeric) %>%
    mutate(sum_exp =  exp(coallik_CDU_CSU_FDP_GRUENE) + exp(coallik_SPD_GRUENE) + exp(coallik_SPD_FDP_GRUENE) + exp(coallik_CDU_CSU_GRUENE) + exp(coallik_SPD_GRUENE_LINKE),
           coallik_CDU_CSU_FDP_GRUENE = exp(coallik_CDU_CSU_FDP_GRUENE)/sum_exp,
           coallik_SPD_GRUENE = exp(coallik_SPD_GRUENE)/sum_exp, 
           coallik_SPD_FDP_GRUENE = exp(coallik_SPD_FDP_GRUENE)/sum_exp,
           coallik_CDU_CSU_GRUENE = exp(coallik_CDU_CSU_GRUENE)/sum_exp,
           coallik_SPD_GRUENE_LINKE = exp(coallik_SPD_GRUENE_LINKE)/sum_exp) %>%
    select(-sum_exp) %>%
    as.matrix()
  
  ##### LINKE #####
  # Coalition evaluation
  rat_coal_LINKE <- Z %>% select(rat_coal_SPD_GRUENE_LINKE) %>%
    mutate_all(as.numeric) %>%
    as.matrix()
  
  # Coalition likelihood, has to be 1 as only one coalition option available
  coal_lik_LINKE <- dt5 %>% select(coallik_SPD_GRUENE_LINKE)  %>%
    mutate_all(as.numeric) %>%
    mutate(sum_exp =  exp(coallik_SPD_GRUENE_LINKE),
           coallik_SPD_GRUENE_LINKE = exp(coallik_SPD_GRUENE_LINKE)/sum_exp) %>%
    select(-sum_exp) %>%
    as.matrix()
  
  # No AfD coalition evaluation item included
  
  # Mean-variance model
  EV <- function(gamma,Z){
    gamma %*% Z
  }
  
  Vari <- function(gamma,Z){
    gamma %*% ((Z - as.numeric(EV(gamma,Z)))^2)
  }
  
  
  N <- nrow(coal_lik_CDU_CSU)
  M_CDU_CSU <- sapply(1:N, function(i) EV(coal_lik_CDU_CSU[i,], rat_coal_CDU_CSU[i,]))
  V_CDU_CSU <- sapply(1:N, function(i) Vari(coal_lik_CDU_CSU[i,], rat_coal_CDU_CSU[i,]))
  
  N <- nrow(coal_lik_SPD)
  M_SPD <- sapply(1:N, function(i) EV(coal_lik_SPD[i,], rat_coal_SPD[i,]))
  V_SPD <- sapply(1:N, function(i) Vari(coal_lik_SPD[i,], rat_coal_SPD[i,]))
  
  N <- nrow(coal_lik_FDP)
  M_FDP <- sapply(1:N, function(i) EV(coal_lik_FDP[i,], rat_coal_FDP[i,]))
  V_FDP <- sapply(1:N, function(i) Vari(coal_lik_FDP[i,], rat_coal_FDP[i,]))
  
  N <- nrow(coal_lik_GRUENE)
  M_GRUENE <- sapply(1:N, function(i) EV(coal_lik_GRUENE[i,], rat_coal_GRUENE[i,]))
  V_GRUENE <- sapply(1:N, function(i) Vari(coal_lik_GRUENE[i,], rat_coal_GRUENE[i,]))
  
  N <- nrow(coal_lik_LINKE)
  M_LINKE <- sapply(1:N, function(i) EV(coal_lik_LINKE[i,], rat_coal_LINKE[i,]))
  V_LINKE <- sapply(1:N, function(i) Vari(coal_lik_LINKE[i,], rat_coal_LINKE[i,])) # is 0
  
  # Add sociodemographic information
  dt5 <- cbind(dt5, dt)
  
  d5 <- data.frame(
    "ptv" = dt5$vote,
    "ptv_UNION" = dt5$ptv_UNION,
    "ptv_SPD" = dt5$ptv_SPD,
    "ptv_FDP" = dt5$ptv_FDP,
    "ptv_GRUENE" = dt5$ptv_GRUENE,
    "ptv_LINKE" = dt5$ptv_LINKE,
    "rat.UNION" = (dt5$rat_CDU + dt5$rat_CSU)/2,
    "rat.SPD" = dt5$rat_SPD,
    "rat.FDP" = dt5$rat_FDP,
    "rat.GRUENE" = dt5$rat_GRUENE,
    "rat.LINKE" = dt5$rat_LINKE,
    "lotterymean.UNION" = M_CDU_CSU,
    "lotteryvariance.UNION" = V_CDU_CSU,
    "lotterymean.SPD" = M_SPD,
    "lotteryvariance.SPD" = V_SPD,
    "lotterymean.FDP" = M_FDP,
    "lotteryvariance.FDP" = V_FDP,
    "lotterymean.GRUENE" = M_GRUENE,
    "lotteryvariance.GRUENE" = V_GRUENE,
    "lotterymean.LINKE" = M_LINKE,
    "lotteryvariance.LINKE" = V_LINKE,
    "sex" = dt5$male,
    "age" = dt5$age,
    "edu" = dt5$kp1_2320
    )
  
  d5$lotteryvariance.UNION <- scales::rescale(d5$lotteryvariance.UNION, to = c(0, 1), from = c(0,0.25))
  d5$lotteryvariance.SPD <- scales::rescale(d5$lotteryvariance.SPD, to = c(0, 1), from = c(0,0.25))
  d5$lotteryvariance.FDP <- scales::rescale(d5$lotteryvariance.FDP, to = c(0, 1), from = c(0,0.25))
  d5$lotteryvariance.GRUENE <- scales::rescale(d5$lotteryvariance.GRUENE, to = c(0, 1), from = c(0,0.25))
  d5$lotteryvariance.LINKE <- scales::rescale(d5$lotteryvariance.LINKE, to = c(0, 1), from = c(0,0.25))
  
  summary(m5a <- lm(ptv_UNION ~ lotteryvariance.UNION + lotterymean.UNION + rat.UNION  + sex + as.factor(edu) + age, d5))
  summary(m5b <- lm(ptv_SPD ~ lotteryvariance.SPD + lotterymean.SPD + rat.SPD + sex + as.factor(edu) + age, d5))
  summary(m5c <- lm(ptv_FDP ~ lotteryvariance.FDP + lotterymean.FDP + rat.FDP + sex + as.factor(edu) + age, d5))
  summary(m5d <- lm(ptv_GRUENE ~ lotteryvariance.GRUENE + lotterymean.GRUENE + rat.GRUENE + sex + as.factor(edu) + age, d5))
  summary(m5e <- lm(ptv_LINKE ~ lotteryvariance.LINKE + lotterymean.LINKE + rat.LINKE + sex + as.factor(edu) + age, d5))
  
  GER_2017_GLES_Panel_w5_UNION_Variance_Estimate <- tidy(m5a) %>% filter(term=="lotteryvariance.UNION") %>% select("estimate")
  GER_2017_GLES_Panel_w5_UNION_Variance_SE <- tidy(m5a) %>% filter(term=="lotteryvariance.UNION") %>% select("std.error")
  GER_2017_GLES_Panel_w5_UNION_Mean_Estimate <- tidy(m5a) %>% filter(term=="lotterymean.UNION") %>% select("estimate")
  GER_2017_GLES_Panel_w5_UNION_Mean_SE <- tidy(m5a) %>% filter(term=="lotterymean.UNION") %>% select("std.error")
  
  GER_2017_GLES_Panel_w5_SPD_Variance_Estimate <- tidy(m5b) %>% filter(term=="lotteryvariance.SPD") %>% select("estimate")
  GER_2017_GLES_Panel_w5_SPD_Variance_SE <- tidy(m5b) %>% filter(term=="lotteryvariance.SPD") %>% select("std.error")
  GER_2017_GLES_Panel_w5_SPD_Mean_Estimate <- tidy(m5b) %>% filter(term=="lotterymean.SPD") %>% select("estimate")
  GER_2017_GLES_Panel_w5_SPD_Mean_SE <- tidy(m5b) %>% filter(term=="lotterymean.SPD") %>% select("std.error")
  
  GER_2017_GLES_Panel_w5_FDP_Variance_Estimate <- tidy(m5c) %>% filter(term=="lotteryvariance.FDP") %>% select("estimate")
  GER_2017_GLES_Panel_w5_FDP_Variance_SE <- tidy(m5c) %>% filter(term=="lotteryvariance.FDP") %>% select("std.error")
  GER_2017_GLES_Panel_w5_FDP_Mean_Estimate <- tidy(m5c) %>% filter(term=="lotterymean.FDP") %>% select("estimate")
  GER_2017_GLES_Panel_w5_FDP_Mean_SE <- tidy(m5c) %>% filter(term=="lotterymean.FDP") %>% select("std.error")
  
  GER_2017_GLES_Panel_w5_GR_Variance_Estimate <- tidy(m5d) %>% filter(term=="lotteryvariance.GRUENE") %>% select("estimate")
  GER_2017_GLES_Panel_w5_GR_Variance_SE <- tidy(m5d) %>% filter(term=="lotteryvariance.GRUENE") %>% select("std.error")
  GER_2017_GLES_Panel_w5_GR_Mean_Estimate <- tidy(m5d) %>% filter(term=="lotterymean.GRUENE") %>% select("estimate")
  GER_2017_GLES_Panel_w5_GR_Mean_SE <- tidy(m5d) %>% filter(term=="lotterymean.GRUENE") %>% select("std.error")
  
  
  
  
  
  
  #### wave 6
  
  dt6 <- data %>% select(contains("kp6_"))
  dt6 <- dt6[,c(1, 6, 10, 39, 91:97, 191:204)]
  
  # Evaluation political parties
  colnames(dt6)[seq(5, 11, 1)] <- c("rat_CDU", "rat_CSU", "rat_SPD", "rat_FDP", "rat_GRUENE", "rat_LINKE", "rat_AfD")
  
  # Coalition evaluation, 
  colnames(dt6)[seq(12, 18, 1)] <- c("rat_coal_CDU_CSU_SPD", "rat_coal_CDU_CSU_FDP", "rat_coal_SPD_GRUENE", "rat_coal_CDU_CSU_GRUENE", "rat_coal_SPD_FDP_GRUENE",  "rat_coal_CDU_CSU_FDP_GRUENE", "rat_coal_SPD_GRUENE_LINKE")
  
  # Coalition likelihood
  colnames(dt6)[seq(19, 25, 1)] <- c("coallik_CDU_CSU_SPD", "coallik_CDU_CSU_FDP", "coallik_SPD_GRUENE", "coallik_CDU_CSU_GRUENE", "coallik_SPD_FDP_GRUENE", "coallik_CDU_CSU_FDP_GRUENE", "coallik_SPD_GRUENE_LINKE")

  # Dependent variable
  vote <- data.frame(dt6$kp6_190bb, dt6$kp6_191bb, dt6$kp6_2770bb)
  
  # Generate party choice variable
  dt6$vote <- dt6$kp6_190bb
  dt6$vote<- ifelse(is.na(dt6$vote), dt6$kp6_191bb, dt6$vote)
  dt6$vote<- ifelse(is.na(dt6$vote), dt6$kp6_2770bb, dt6$vote)

  # Assign interpretable labels to vote variable
  dt6$vote[dt6$vote==1] <- "UNION"
  dt6$vote[dt6$vote==4] <- "SPD"
  dt6$vote[dt6$vote==5] <- "FDP"
  dt6$vote[dt6$vote==6] <- "GRUENE"
  dt6$vote[dt6$vote==7] <- "LINKE"
  dt6$vote[dt6$vote==322] <- "AfD"
  
  ## Generate ptv columns based on vote variable
  dt6$ptv_UNION <- ifelse(dt6$vote == "UNION", 1, 0)
  dt6$ptv_SPD <- ifelse(dt6$vote == "SPD", 1, 0)
  dt6$ptv_FDP <- ifelse(dt6$vote == "FDP", 1, 0)
  dt6$ptv_GRUENE <- ifelse(dt6$vote == "GRUENE", 1, 0)
  dt6$ptv_LINKE <- ifelse(dt6$vote == "LINKE", 1, 0)
  dt6$ptv_AfD <- ifelse(dt6$vote == "AfD", 1, 0)
  
  Z <- dt6 %>% select("rat_coal_CDU_CSU_SPD", "rat_coal_CDU_CSU_FDP", "rat_coal_SPD_GRUENE" , "rat_coal_CDU_CSU_GRUENE", "rat_coal_SPD_FDP_GRUENE", "rat_coal_CDU_CSU_FDP_GRUENE", "rat_coal_SPD_GRUENE_LINKE") %>%
    mutate_all(as.numeric) %>%
    mutate_all(funs(scales::rescale(.,to = c(0, 1)))) 
  
  ##### CDU/CSU #####
  # Coalition evaluation
  rat_coal_CDU_CSU <- Z %>% select(-c(rat_coal_SPD_GRUENE, rat_coal_SPD_FDP_GRUENE, rat_coal_SPD_GRUENE_LINKE)) %>%
    mutate_all(as.numeric) %>%
    as.matrix()
  
  # Coalition likelihood
  coal_lik_CDU_CSU <- dt6 %>% select(coallik_CDU_CSU_SPD, coallik_CDU_CSU_FDP, coallik_CDU_CSU_FDP_GRUENE, coallik_CDU_CSU_GRUENE)  %>%
    mutate_all(as.numeric) %>%
    mutate(sum_exp = exp(coallik_CDU_CSU_SPD) + exp(coallik_CDU_CSU_FDP) + exp(coallik_CDU_CSU_FDP_GRUENE) + exp(coallik_CDU_CSU_GRUENE),
           coallik_CDU_CSU_SPD = exp(coallik_CDU_CSU_SPD)/sum_exp,
           coallik_CDU_CSU_FDP = exp(coallik_CDU_CSU_FDP)/sum_exp,
           coallik_CDU_CSU_FDP_GRUENE = exp(coallik_CDU_CSU_FDP_GRUENE)/sum_exp,
           coallik_CDU_CSU_GRUENE = exp(coallik_CDU_CSU_GRUENE)/sum_exp) %>%
    select(-sum_exp) %>%
    as.matrix()
  
  ##### SPD #####
  # Coalition evaluation
  rat_coal_SPD <- Z %>% select(-c(rat_coal_CDU_CSU_FDP, rat_coal_CDU_CSU_GRUENE, rat_coal_CDU_CSU_FDP_GRUENE)) %>%
    mutate_all(as.numeric) %>%
    as.matrix()
  
  # Coalition likelihood
  coal_lik_SPD <- dt6 %>% select(coallik_CDU_CSU_SPD, coallik_SPD_GRUENE, coallik_SPD_FDP_GRUENE, coallik_SPD_GRUENE_LINKE)  %>%
    mutate_all(as.numeric) %>%
    mutate(sum_exp = exp(coallik_CDU_CSU_SPD) + exp(coallik_SPD_GRUENE) + exp(coallik_SPD_FDP_GRUENE) + exp(coallik_SPD_GRUENE_LINKE),
           coallik_CDU_CSU_SPD = exp(coallik_CDU_CSU_SPD)/sum_exp,
           coallik_SPD_GRUENE = exp(coallik_SPD_GRUENE)/sum_exp,
           coallik_SPD_FDP_GRUENE = exp(coallik_SPD_FDP_GRUENE)/sum_exp,
           coallik_SPD_GRUENE_LINKE = exp(coallik_SPD_GRUENE_LINKE)/sum_exp) %>%
    select(-sum_exp) %>%
    as.matrix()
  
  ##### FDP #####
  # Coalition evaluation
  rat_coal_FDP <- Z %>% select(-c(rat_coal_CDU_CSU_SPD, rat_coal_SPD_GRUENE, rat_coal_CDU_CSU_GRUENE, rat_coal_SPD_GRUENE_LINKE)) %>%
    mutate_all(as.numeric) %>%
    as.matrix()
  
  # Coalition likelihood
  coal_lik_FDP <- dt6 %>% select(coallik_CDU_CSU_FDP, coallik_SPD_FDP_GRUENE, coallik_CDU_CSU_FDP_GRUENE)  %>%
    mutate_all(as.numeric) %>%
    mutate(sum_exp = exp(coallik_CDU_CSU_FDP) + exp(coallik_SPD_FDP_GRUENE) + exp(coallik_CDU_CSU_FDP_GRUENE),
           coallik_CDU_CSU_FDP = exp(coallik_CDU_CSU_FDP)/sum_exp,
           coallik_SPD_FDP_GRUENE = exp(coallik_SPD_FDP_GRUENE)/sum_exp,
           coallik_CDU_CSU_FDP_GRUENE = exp(coallik_CDU_CSU_FDP_GRUENE)/sum_exp) %>%
    select(-sum_exp) %>%
    as.matrix()
  
  ##### GRUENE #####
  # Coalition evaluation
  rat_coal_GRUENE <- Z %>% select(-c(rat_coal_CDU_CSU_SPD, rat_coal_CDU_CSU_FDP)) %>%
    mutate_all(as.numeric) %>%
    as.matrix()
  
  # Coalition likelihood
  coal_lik_GRUENE <- dt6 %>% select(coallik_CDU_CSU_FDP_GRUENE, coallik_SPD_GRUENE, coallik_SPD_FDP_GRUENE, coallik_CDU_CSU_GRUENE, coallik_SPD_GRUENE_LINKE)  %>%
    mutate_all(as.numeric) %>%
    mutate(sum_exp =  exp(coallik_CDU_CSU_FDP_GRUENE) + exp(coallik_SPD_GRUENE) + exp(coallik_SPD_FDP_GRUENE) + exp(coallik_CDU_CSU_GRUENE) + exp(coallik_SPD_GRUENE_LINKE),
           coallik_CDU_CSU_FDP_GRUENE = exp(coallik_CDU_CSU_FDP_GRUENE)/sum_exp,
           coallik_SPD_GRUENE = exp(coallik_SPD_GRUENE)/sum_exp, 
           coallik_SPD_FDP_GRUENE = exp(coallik_SPD_FDP_GRUENE)/sum_exp,
           coallik_CDU_CSU_GRUENE = exp(coallik_CDU_CSU_GRUENE)/sum_exp,
           coallik_SPD_GRUENE_LINKE = exp(coallik_SPD_GRUENE_LINKE)/sum_exp) %>%
    select(-sum_exp) %>%
    as.matrix()
  
  ##### LINKE #####
  # Coalition evaluation
  rat_coal_LINKE <- Z %>% select(rat_coal_SPD_GRUENE_LINKE) %>%
    mutate_all(as.numeric) %>%
    as.matrix()
  
  # Coalition likelihood, has to be 1 as only one coalition option available
  coal_lik_LINKE <- dt6 %>% select(coallik_SPD_GRUENE_LINKE)  %>%
    mutate_all(as.numeric) %>%
    mutate(sum_exp =  exp(coallik_SPD_GRUENE_LINKE),
           coallik_SPD_GRUENE_LINKE = exp(coallik_SPD_GRUENE_LINKE)/sum_exp) %>%
    select(-sum_exp) %>%
    as.matrix()
  
  # No AfD coalition evaluation item included
  
  # Mean-variance model
  EV <- function(gamma,Z){
    gamma %*% Z
  }
  
  Vari <- function(gamma,Z){
    gamma %*% ((Z - as.numeric(EV(gamma,Z)))^2)
  }
  
  
  N <- nrow(coal_lik_CDU_CSU)
  M_CDU_CSU <- sapply(1:N, function(i) EV(coal_lik_CDU_CSU[i,], rat_coal_CDU_CSU[i,]))
  V_CDU_CSU <- sapply(1:N, function(i) Vari(coal_lik_CDU_CSU[i,], rat_coal_CDU_CSU[i,]))
  
  N <- nrow(coal_lik_SPD)
  M_SPD <- sapply(1:N, function(i) EV(coal_lik_SPD[i,], rat_coal_SPD[i,]))
  V_SPD <- sapply(1:N, function(i) Vari(coal_lik_SPD[i,], rat_coal_SPD[i,]))
  
  N <- nrow(coal_lik_FDP)
  M_FDP <- sapply(1:N, function(i) EV(coal_lik_FDP[i,], rat_coal_FDP[i,]))
  V_FDP <- sapply(1:N, function(i) Vari(coal_lik_FDP[i,], rat_coal_FDP[i,]))
  
  N <- nrow(coal_lik_GRUENE)
  M_GRUENE <- sapply(1:N, function(i) EV(coal_lik_GRUENE[i,], rat_coal_GRUENE[i,]))
  V_GRUENE <- sapply(1:N, function(i) Vari(coal_lik_GRUENE[i,], rat_coal_GRUENE[i,]))
  
  N <- nrow(coal_lik_LINKE)
  M_LINKE <- sapply(1:N, function(i) EV(coal_lik_LINKE[i,], rat_coal_LINKE[i,]))
  V_LINKE <- sapply(1:N, function(i) Vari(coal_lik_LINKE[i,], rat_coal_LINKE[i,])) # is 0
  
  # Add sociodemographic information
  dt6 <- cbind(dt6, dt)
  
  d6 <- data.frame(
    "ptv" = dt6$vote,
    "ptv_UNION" = dt6$ptv_UNION,
    "ptv_SPD" = dt6$ptv_SPD,
    "ptv_FDP" = dt6$ptv_FDP,
    "ptv_GRUENE" = dt6$ptv_GRUENE,
    "ptv_LINKE" = dt6$ptv_LINKE,
    "rat.UNION" = (dt6$rat_CDU + dt6$rat_CSU)/2,
    "rat.SPD" = dt6$rat_SPD,
    "rat.FDP" = dt6$rat_FDP,
    "rat.GRUENE" = dt6$rat_GRUENE,
    "rat.LINKE" = dt6$rat_LINKE,
    "lotterymean.UNION" = M_CDU_CSU,
    "lotteryvariance.UNION" = V_CDU_CSU,
    "lotterymean.SPD" = M_SPD,
    "lotteryvariance.SPD" = V_SPD,
    "lotterymean.FDP" = M_FDP,
    "lotteryvariance.FDP" = V_FDP,
    "lotterymean.GRUENE" = M_GRUENE,
    "lotteryvariance.GRUENE" = V_GRUENE,
    "lotterymean.LINKE" = M_LINKE,
    "lotteryvariance.LINKE" = V_LINKE,
    "sex" = dt6$male,
    "age" = dt6$age,
    "edu" = dt6$kp1_2320
  )
  
  d6$lotteryvariance.UNION <- scales::rescale(d6$lotteryvariance.UNION, to = c(0, 1), from = c(0,0.25))
  d6$lotteryvariance.SPD <- scales::rescale(d6$lotteryvariance.SPD, to = c(0, 1), from = c(0,0.25))
  d6$lotteryvariance.FDP <- scales::rescale(d6$lotteryvariance.FDP, to = c(0, 1), from = c(0,0.25))
  d6$lotteryvariance.GRUENE <- scales::rescale(d6$lotteryvariance.GRUENE, to = c(0, 1), from = c(0,0.25))
  d6$lotteryvariance.LINKE <- scales::rescale(d6$lotteryvariance.LINKE, to = c(0, 1), from = c(0,0.25))
  
# Save model results
  
  summary(m6a <- lm(ptv_UNION ~ lotteryvariance.UNION + lotterymean.UNION + rat.UNION  + sex + as.factor(edu) + age, d6))
  summary(m6b <- lm(ptv_SPD ~ lotteryvariance.SPD + lotterymean.SPD + rat.SPD + sex + as.factor(edu) + age, d6))
  summary(m6c <- lm(ptv_FDP ~ lotteryvariance.FDP + lotterymean.FDP + rat.FDP + sex + as.factor(edu) + age, d6))
  summary(m6d <- lm(ptv_GRUENE ~ lotteryvariance.GRUENE + lotterymean.GRUENE + rat.GRUENE + sex + as.factor(edu) + age, d6))
  summary(m6e <- lm(ptv_LINKE ~ lotteryvariance.LINKE + lotterymean.LINKE + rat.LINKE + sex + as.factor(edu) + age, d6))
  
  GER_2017_GLES_Panel_w6_UNION_Variance_Estimate <- tidy(m6a) %>% filter(term=="lotteryvariance.UNION") %>% select("estimate")
  GER_2017_GLES_Panel_w6_UNION_Variance_SE <- tidy(m6a) %>% filter(term=="lotteryvariance.UNION") %>% select("std.error")
  GER_2017_GLES_Panel_w6_UNION_Mean_Estimate <- tidy(m6a) %>% filter(term=="lotterymean.UNION") %>% select("estimate")
  GER_2017_GLES_Panel_w6_UNION_Mean_SE <- tidy(m6a) %>% filter(term=="lotterymean.UNION") %>% select("std.error")
  
  GER_2017_GLES_Panel_w6_SPD_Variance_Estimate <- tidy(m6b) %>% filter(term=="lotteryvariance.SPD") %>% select("estimate")
  GER_2017_GLES_Panel_w6_SPD_Variance_SE <- tidy(m6b) %>% filter(term=="lotteryvariance.SPD") %>% select("std.error")
  GER_2017_GLES_Panel_w6_SPD_Mean_Estimate <- tidy(m6b) %>% filter(term=="lotterymean.SPD") %>% select("estimate")
  GER_2017_GLES_Panel_w6_SPD_Mean_SE <- tidy(m6b) %>% filter(term=="lotterymean.SPD") %>% select("std.error")
  
  GER_2017_GLES_Panel_w6_FDP_Variance_Estimate <- tidy(m6c) %>% filter(term=="lotteryvariance.FDP") %>% select("estimate")
  GER_2017_GLES_Panel_w6_FDP_Variance_SE <- tidy(m6c) %>% filter(term=="lotteryvariance.FDP") %>% select("std.error")
  GER_2017_GLES_Panel_w6_FDP_Mean_Estimate <- tidy(m6c) %>% filter(term=="lotterymean.FDP") %>% select("estimate")
  GER_2017_GLES_Panel_w6_FDP_Mean_SE <- tidy(m6c) %>% filter(term=="lotterymean.FDP") %>% select("std.error")
  
  GER_2017_GLES_Panel_w6_GR_Variance_Estimate <- tidy(m6d) %>% filter(term=="lotteryvariance.GRUENE") %>% select("estimate")
  GER_2017_GLES_Panel_w6_GR_Variance_SE <- tidy(m6d) %>% filter(term=="lotteryvariance.GRUENE") %>% select("std.error")
  GER_2017_GLES_Panel_w6_GR_Mean_Estimate <- tidy(m6d) %>% filter(term=="lotterymean.GRUENE") %>% select("estimate")
  GER_2017_GLES_Panel_w6_GR_Mean_SE <- tidy(m6d) %>% filter(term=="lotterymean.GRUENE") %>% select("std.error")
  
  
  
  
  
  
  
  
  
  
  
  
  #### wave 7
  
  dt7 <- data %>% select(contains("kp7_"))
  dt7 <- dt7[,c(1, 6, 10, 39, 87:93, 221:234)]
  
  # Evaluation political parties
  colnames(dt7)[seq(5, 11, 1)] <- c("rat_CDU", "rat_CSU", "rat_SPD", "rat_FDP", "rat_GRUENE", "rat_LINKE", "rat_AfD")
  
  # Coalition evaluation
  colnames(dt7)[seq(12, 18, 1)] <- c("rat_coal_CDU_CSU_SPD", "rat_coal_CDU_CSU_FDP", "rat_coal_SPD_GRUENE", "rat_coal_CDU_CSU_GRUENE", "rat_coal_SPD_FDP_GRUENE",  "rat_coal_CDU_CSU_FDP_GRUENE", "rat_coal_SPD_GRUENE_LINKE")
  
  # Coalition likelihood
  colnames(dt7)[seq(19, 25, 1)] <- c("coallik_CDU_CSU_SPD", "coallik_CDU_CSU_FDP", "coallik_SPD_GRUENE", "coallik_CDU_CSU_GRUENE", "coallik_SPD_FDP_GRUENE", "coallik_CDU_CSU_FDP_GRUENE", "coallik_SPD_GRUENE_LINKE")

  # Generate party choice variable
  dt7$vote <- dt7$kp7_190bb
  dt7$vote<- ifelse(is.na(dt7$vote), dt7$kp7_191bb, dt7$vote)
  dt7$vote<- ifelse(is.na(dt7$vote), dt7$kp7_2770bb, dt7$vote)

  # Assign interpretable labels to vote variable
  dt7$vote[dt7$vote==1] <- "UNION"
  dt7$vote[dt7$vote==4] <- "SPD"
  dt7$vote[dt7$vote==5] <- "FDP"
  dt7$vote[dt7$vote==6] <- "GRUENE"
  dt7$vote[dt7$vote==7] <- "LINKE"
  dt7$vote[dt7$vote==322] <- "AfD"
  
  # Generate ptv columns based on vote variable
  dt7$ptv_UNION <- ifelse(dt7$vote == "UNION", 1, 0)
  dt7$ptv_SPD <- ifelse(dt7$vote == "SPD", 1, 0)
  dt7$ptv_FDP <- ifelse(dt7$vote == "FDP", 1, 0)
  dt7$ptv_GRUENE <- ifelse(dt7$vote == "GRUENE", 1, 0)
  dt7$ptv_LINKE <- ifelse(dt7$vote == "LINKE", 1, 0)
  dt7$ptv_AfD <- ifelse(dt7$vote == "AfD", 1, 0)
  
  Z <- dt7 %>% select("rat_coal_CDU_CSU_SPD", "rat_coal_CDU_CSU_FDP", "rat_coal_SPD_GRUENE" , "rat_coal_CDU_CSU_GRUENE", "rat_coal_SPD_FDP_GRUENE", "rat_coal_CDU_CSU_FDP_GRUENE", "rat_coal_SPD_GRUENE_LINKE") %>%
    mutate_all(as.numeric) %>%
    mutate_all(funs(scales::rescale(.,to = c(0, 1)))) 
  
  ##### CDU/CSU #####
  # Coalition evaluation
  rat_coal_CDU_CSU <- Z %>% select(-c(rat_coal_SPD_GRUENE, rat_coal_SPD_FDP_GRUENE, rat_coal_SPD_GRUENE_LINKE)) %>%
    mutate_all(as.numeric) %>%
    as.matrix()
  
  # Coalition likelihood
  coal_lik_CDU_CSU <- dt7 %>% select(coallik_CDU_CSU_SPD, coallik_CDU_CSU_FDP, coallik_CDU_CSU_FDP_GRUENE, coallik_CDU_CSU_GRUENE)  %>%
    mutate_all(as.numeric) %>%
    mutate(sum_exp = exp(coallik_CDU_CSU_SPD) + exp(coallik_CDU_CSU_FDP) + exp(coallik_CDU_CSU_FDP_GRUENE) + exp(coallik_CDU_CSU_GRUENE),
           coallik_CDU_CSU_SPD = exp(coallik_CDU_CSU_SPD)/sum_exp,
           coallik_CDU_CSU_FDP = exp(coallik_CDU_CSU_FDP)/sum_exp,
           coallik_CDU_CSU_FDP_GRUENE = exp(coallik_CDU_CSU_FDP_GRUENE)/sum_exp,
           coallik_CDU_CSU_GRUENE = exp(coallik_CDU_CSU_GRUENE)/sum_exp) %>%
    select(-sum_exp) %>%
    as.matrix()
  
  ##### SPD #####
  # Coalition evaluation
  rat_coal_SPD <- Z %>% select(-c(rat_coal_CDU_CSU_FDP, rat_coal_CDU_CSU_GRUENE, rat_coal_CDU_CSU_FDP_GRUENE)) %>%
    mutate_all(as.numeric) %>%
    as.matrix()
  
  # Coalition likelihood
  coal_lik_SPD <- dt7 %>% select(coallik_CDU_CSU_SPD, coallik_SPD_GRUENE, coallik_SPD_FDP_GRUENE, coallik_SPD_GRUENE_LINKE)  %>%
    mutate_all(as.numeric) %>%
    mutate(sum_exp = exp(coallik_CDU_CSU_SPD) + exp(coallik_SPD_GRUENE) + exp(coallik_SPD_FDP_GRUENE) + exp(coallik_SPD_GRUENE_LINKE),
           coallik_CDU_CSU_SPD = exp(coallik_CDU_CSU_SPD)/sum_exp,
           coallik_SPD_GRUENE = exp(coallik_SPD_GRUENE)/sum_exp,
           coallik_SPD_FDP_GRUENE = exp(coallik_SPD_FDP_GRUENE)/sum_exp,
           coallik_SPD_GRUENE_LINKE = exp(coallik_SPD_GRUENE_LINKE)/sum_exp) %>%
    select(-sum_exp) %>%
    as.matrix()
  
  ##### FDP #####
  # Coalition evaluation
  rat_coal_FDP <- Z %>% select(-c(rat_coal_CDU_CSU_SPD, rat_coal_SPD_GRUENE, rat_coal_CDU_CSU_GRUENE, rat_coal_SPD_GRUENE_LINKE)) %>%
    mutate_all(as.numeric) %>%
    as.matrix()
  
  # Coalition likelihood
  coal_lik_FDP <- dt7 %>% select(coallik_CDU_CSU_FDP, coallik_SPD_FDP_GRUENE, coallik_CDU_CSU_FDP_GRUENE)  %>%
    mutate_all(as.numeric) %>%
    mutate(sum_exp = exp(coallik_CDU_CSU_FDP) + exp(coallik_SPD_FDP_GRUENE) + exp(coallik_CDU_CSU_FDP_GRUENE),
           coallik_CDU_CSU_FDP = exp(coallik_CDU_CSU_FDP)/sum_exp,
           coallik_SPD_FDP_GRUENE = exp(coallik_SPD_FDP_GRUENE)/sum_exp,
           coallik_CDU_CSU_FDP_GRUENE = exp(coallik_CDU_CSU_FDP_GRUENE)/sum_exp) %>%
    select(-sum_exp) %>%
    as.matrix()
  
  ##### GRUENE #####
  # Coalition evaluation
  rat_coal_GRUENE <- Z %>% select(-c(rat_coal_CDU_CSU_SPD, rat_coal_CDU_CSU_FDP)) %>%
    mutate_all(as.numeric) %>%
    as.matrix()
  
  # Coalition likelihood
  coal_lik_GRUENE <- dt7 %>% select(coallik_CDU_CSU_FDP_GRUENE, coallik_SPD_GRUENE, coallik_SPD_FDP_GRUENE, coallik_CDU_CSU_GRUENE, coallik_SPD_GRUENE_LINKE)  %>%
    mutate_all(as.numeric) %>%
    mutate(sum_exp =  exp(coallik_CDU_CSU_FDP_GRUENE) + exp(coallik_SPD_GRUENE) + exp(coallik_SPD_FDP_GRUENE) + exp(coallik_CDU_CSU_GRUENE) + exp(coallik_SPD_GRUENE_LINKE),
           coallik_CDU_CSU_FDP_GRUENE = exp(coallik_CDU_CSU_FDP_GRUENE)/sum_exp,
           coallik_SPD_GRUENE = exp(coallik_SPD_GRUENE)/sum_exp, 
           coallik_SPD_FDP_GRUENE = exp(coallik_SPD_FDP_GRUENE)/sum_exp,
           coallik_CDU_CSU_GRUENE = exp(coallik_CDU_CSU_GRUENE)/sum_exp,
           coallik_SPD_GRUENE_LINKE = exp(coallik_SPD_GRUENE_LINKE)/sum_exp) %>%
    select(-sum_exp) %>%
    as.matrix()
  
  ##### LINKE #####
  # Coalition evaluation
  rat_coal_LINKE <- Z %>% select(rat_coal_SPD_GRUENE_LINKE) %>%
    mutate_all(as.numeric) %>%
    as.matrix()
  
  # Coalition likelihood, has to be 1 as only one coalition option available
  coal_lik_LINKE <- dt7 %>% select(coallik_SPD_GRUENE_LINKE)  %>%
    mutate_all(as.numeric) %>%
    mutate(sum_exp =  exp(coallik_SPD_GRUENE_LINKE),
           coallik_SPD_GRUENE_LINKE = exp(coallik_SPD_GRUENE_LINKE)/sum_exp) %>%
    select(-sum_exp) %>%
    as.matrix()
  
  # No AfD coalition evaluation item included
  
  # Mean-variance model
  EV <- function(gamma,Z){
    gamma %*% Z
  }
  
  Vari <- function(gamma,Z){
    gamma %*% ((Z - as.numeric(EV(gamma,Z)))^2)
  }
  
  N <- nrow(coal_lik_CDU_CSU)
  M_CDU_CSU <- sapply(1:N, function(i) EV(coal_lik_CDU_CSU[i,], rat_coal_CDU_CSU[i,]))
  V_CDU_CSU <- sapply(1:N, function(i) Vari(coal_lik_CDU_CSU[i,], rat_coal_CDU_CSU[i,]))
  
  N <- nrow(coal_lik_SPD)
  M_SPD <- sapply(1:N, function(i) EV(coal_lik_SPD[i,], rat_coal_SPD[i,]))
  V_SPD <- sapply(1:N, function(i) Vari(coal_lik_SPD[i,], rat_coal_SPD[i,]))
  
  N <- nrow(coal_lik_FDP)
  M_FDP <- sapply(1:N, function(i) EV(coal_lik_FDP[i,], rat_coal_FDP[i,]))
  V_FDP <- sapply(1:N, function(i) Vari(coal_lik_FDP[i,], rat_coal_FDP[i,]))
  
  N <- nrow(coal_lik_GRUENE)
  M_GRUENE <- sapply(1:N, function(i) EV(coal_lik_GRUENE[i,], rat_coal_GRUENE[i,]))
  V_GRUENE <- sapply(1:N, function(i) Vari(coal_lik_GRUENE[i,], rat_coal_GRUENE[i,]))
  
  N <- nrow(coal_lik_LINKE)
  M_LINKE <- sapply(1:N, function(i) EV(coal_lik_LINKE[i,], rat_coal_LINKE[i,]))
  V_LINKE <- sapply(1:N, function(i) Vari(coal_lik_LINKE[i,], rat_coal_LINKE[i,])) # is 0
  
  # Add sociodemographic information
  dt7 <- cbind(dt7, dt)
  
  # PID
  if (!exists("robustnesscheck_pid")) {
    robustnesscheck_pid <- FALSE # PID as robustness?
  }
  dt7$pid_UNION <- ifelse(data$kp7_2090a==1|data$kp7_2090a==2|data$kp7_2090a==3, 1, 0)
  dt7$pid_SPD <- ifelse(data$kp7_2090a==4, 1, 0)
  dt7$pid_FDP <- ifelse(data$kp7_2090a==5, 1, 0)
  dt7$pid_GRUENE <- ifelse(data$kp7_2090a==6, 1, 0)
  dt7$pid_LINKE <- ifelse(data$kp7_2090a==7, 1, 0)
  
  d7 <- data.frame(
    "ptv" = dt7$vote,
    "ptv_UNION" = dt7$ptv_UNION,
    "ptv_SPD" = dt7$ptv_SPD,
    "ptv_FDP" = dt7$ptv_FDP,
    "ptv_GRUENE" = dt7$ptv_GRUENE,
    "ptv_LINKE" = dt7$ptv_LINKE,
    "rat.UNION" = (dt7$rat_CDU + dt7$rat_CSU)/2,
    "rat.SPD" = dt7$rat_SPD,
    "rat.FDP" = dt7$rat_FDP,
    "rat.GRUENE" = dt7$rat_GRUENE,
    "rat.LINKE" = dt7$rat_LINKE,
    "pid_UNION" = dt7$pid_UNION,
    "pid_SPD" = dt7$pid_SPD,
    "pid_FDP" = dt7$pid_FDP,
    "pid_GRUENE" = dt7$pid_GRUENE,
    "pid_LINKE" = dt7$pid_LINKE,
    "lotterymean.UNION" = M_CDU_CSU,
    "lotteryvariance.UNION" = V_CDU_CSU,
    "lotterymean.SPD" = M_SPD,
    "lotteryvariance.SPD" = V_SPD,
    "lotterymean.FDP" = M_FDP,
    "lotteryvariance.FDP" = V_FDP,
    "lotterymean.GRUENE" = M_GRUENE,
    "lotteryvariance.GRUENE" = V_GRUENE,
    "lotterymean.LINKE" = M_LINKE,
    "lotteryvariance.LINKE" = V_LINKE,
    "sex" = dt7$male,
    "age" = dt7$age,
    "edu" = dt7$kp1_2320
    )
  
  d7$lotteryvariance.UNION <- scales::rescale(d7$lotteryvariance.UNION, to = c(0, 1), from = c(0,0.25))
  d7$lotteryvariance.SPD <- scales::rescale(d7$lotteryvariance.SPD, to = c(0, 1), from = c(0,0.25))
  d7$lotteryvariance.FDP <- scales::rescale(d7$lotteryvariance.FDP, to = c(0, 1), from = c(0,0.25))
  d7$lotteryvariance.GRUENE <- scales::rescale(d7$lotteryvariance.GRUENE, to = c(0, 1), from = c(0,0.25))
  d7$lotteryvariance.LINKE <- scales::rescale(d7$lotteryvariance.LINKE, to = c(0, 1), from = c(0,0.25))
  
# Save model results
  summary(m7a <- lm(as.formula(paste("ptv_UNION ~ lotteryvariance.UNION + lotterymean.UNION + rat.UNION  + sex + as.factor(edu) + age", 
                                     if (robustnesscheck_pid) "+ pid_UNION" else "")), d7))
  summary(m7b <- lm(as.formula(paste("ptv_SPD ~ lotteryvariance.SPD + lotterymean.SPD + rat.SPD + sex + as.factor(edu) + age", 
                                     if (robustnesscheck_pid) "+ pid_SPD" else "")), d7))
  summary(m7c <- lm(as.formula(paste("ptv_FDP ~ lotteryvariance.FDP + lotterymean.FDP + rat.FDP + sex + as.factor(edu) + age", 
                                     if (robustnesscheck_pid) "+ pid_FDP" else "")), d7))
  summary(m7d <- lm(as.formula(paste("ptv_GRUENE ~ lotteryvariance.GRUENE + lotterymean.GRUENE + rat.GRUENE + sex + as.factor(edu) + age", 
                                     if (robustnesscheck_pid) "+ pid_GRUENE" else "")), d7))
  summary(m7e <- lm(as.formula(paste("ptv_LINKE ~ lotteryvariance.LINKE + lotterymean.LINKE + rat.LINKE + sex + as.factor(edu) + age", 
                                     if (robustnesscheck_pid) "+ pid_LINKE" else "")), d7))
  
  GER_2017_GLES_Panel_w7_UNION_Variance_Estimate <- tidy(m7a) %>% filter(term=="lotteryvariance.UNION") %>% select("estimate")
  GER_2017_GLES_Panel_w7_UNION_Variance_SE <- se_robust(m7a)["lotteryvariance.UNION"] #tidy(m7a) %>% filter(term=="lotteryvariance.UNION") %>% select("std.error")
  GER_2017_GLES_Panel_w7_UNION_Mean_Estimate <- tidy(m7a) %>% filter(term=="lotterymean.UNION") %>% select("estimate")
  GER_2017_GLES_Panel_w7_UNION_Mean_SE <- se_robust(m7a)["lotterymean.UNION"] #tidy(m7a) %>% filter(term=="lotterymean.UNION") %>% select("std.error")
  
  GER_2017_GLES_Panel_w7_SPD_Variance_Estimate <- tidy(m7b) %>% filter(term=="lotteryvariance.SPD") %>% select("estimate")
  GER_2017_GLES_Panel_w7_SPD_Variance_SE <- se_robust(m7b)["lotteryvariance.SPD"] #tidy(m7b) %>% filter(term=="lotteryvariance.SPD") %>% select("std.error")
  GER_2017_GLES_Panel_w7_SPD_Mean_Estimate <- tidy(m7b) %>% filter(term=="lotterymean.SPD") %>% select("estimate")
  GER_2017_GLES_Panel_w7_SPD_Mean_SE <- se_robust(m7b)["lotterymean.SPD"] #tidy(m7b) %>% filter(term=="lotterymean.SPD") %>% select("std.error")
  
  GER_2017_GLES_Panel_w7_FDP_Variance_Estimate <- tidy(m7c) %>% filter(term=="lotteryvariance.FDP") %>% select("estimate")
  GER_2017_GLES_Panel_w7_FDP_Variance_SE <- se_robust(m7c)["lotteryvariance.FDP"] #tidy(m7c) %>% filter(term=="lotteryvariance.FDP") %>% select("std.error")
  GER_2017_GLES_Panel_w7_FDP_Mean_Estimate <- tidy(m7c) %>% filter(term=="lotterymean.FDP") %>% select("estimate")
  GER_2017_GLES_Panel_w7_FDP_Mean_SE <- se_robust(m7c)["lotterymean.FDP"] #tidy(m7c) %>% filter(term=="lotterymean.FDP") %>% select("std.error")
  
  GER_2017_GLES_Panel_w7_GR_Variance_Estimate <- tidy(m7d) %>% filter(term=="lotteryvariance.GRUENE") %>% select("estimate")
  GER_2017_GLES_Panel_w7_GR_Variance_SE <- se_robust(m7d)["lotteryvariance.GRUENE"] #tidy(m7d) %>% filter(term=="lotteryvariance.GRUENE") %>% select("std.error")
  GER_2017_GLES_Panel_w7_GR_Mean_Estimate <- tidy(m7d) %>% filter(term=="lotterymean.GRUENE") %>% select("estimate")
  GER_2017_GLES_Panel_w7_GR_Mean_SE <- se_robust(m7d)["lotterymean.GRUENE"] #tidy(m7d) %>% filter(term=="lotterymean.GRUENE") %>% select("std.error")
  
  #### Summary of results
  # Harmonize names of the two IVs of interest in the models
  
  # Wave 3, 2017
  names(m3a$coefficients)[names(m3a$coefficients) == "lotteryvariance.UNION"] <- "Government Lottery Variance"
  names(m3a$coefficients)[names(m3a$coefficients) == "lotterymean.UNION"] <- "Government Lottery Mean"
  names(m3b$coefficients)[names(m3b$coefficients) == "lotteryvariance.SPD"] <- "Government Lottery Variance"
  names(m3b$coefficients)[names(m3b$coefficients) == "lotterymean.SPD"] <- "Government Lottery Mean"
  names(m3c$coefficients)[names(m3c$coefficients) == "lotteryvariance.FDP"] <- "Government Lottery Variance"
  names(m3c$coefficients)[names(m3c$coefficients) == "lotterymean.FDP"] <- "Government Lottery Mean"
  names(m3d$coefficients)[names(m3d$coefficients) == "lotteryvariance.GRUENE"] <- "Government Lottery Variance"
  names(m3d$coefficients)[names(m3d$coefficients) == "lotterymean.GRUENE"] <- "Government Lottery Mean"
  names(m3e$coefficients)[names(m3e$coefficients) == "lotteryvariance.LINKE"] <- "Government Lottery Variance"
  names(m3e$coefficients)[names(m3e$coefficients) == "lotterymean.LINKE"] <- "Government Lottery Mean"
  
  
  # Wave 5, 2017
  names(m5a$coefficients)[names(m5a$coefficients) == "lotteryvariance.UNION"] <- "Government Lottery Variance"
  names(m5a$coefficients)[names(m5a$coefficients) == "lotterymean.UNION"] <- "Government Lottery Mean"
  names(m5b$coefficients)[names(m5b$coefficients) == "lotteryvariance.SPD"] <- "Government Lottery Variance"
  names(m5b$coefficients)[names(m5b$coefficients) == "lotterymean.SPD"] <- "Government Lottery Mean"
  names(m5c$coefficients)[names(m5c$coefficients) == "lotteryvariance.FDP"] <- "Government Lottery Variance"
  names(m5c$coefficients)[names(m5c$coefficients) == "lotterymean.FDP"] <- "Government Lottery Mean"
  names(m5d$coefficients)[names(m5d$coefficients) == "lotteryvariance.GRUENE"] <- "Government Lottery Variance"
  names(m5d$coefficients)[names(m5d$coefficients) == "lotterymean.GRUENE"] <- "Government Lottery Mean"
  names(m5e$coefficients)[names(m5e$coefficients) == "lotteryvariance.LINKE"] <- "Government Lottery Variance"
  names(m5e$coefficients)[names(m5e$coefficients) == "lotterymean.LINKE"] <- "Government Lottery Mean"
  
  
  # Wave 6, 2017
  names(m6a$coefficients)[names(m6a$coefficients) == "lotteryvariance.UNION"] <- "Government Lottery Variance"
  names(m6a$coefficients)[names(m6a$coefficients) == "lotterymean.UNION"] <- "Government Lottery Mean"
  names(m6b$coefficients)[names(m6b$coefficients) == "lotteryvariance.SPD"] <- "Government Lottery Variance"
  names(m6b$coefficients)[names(m6b$coefficients) == "lotterymean.SPD"] <- "Government Lottery Mean"
  names(m6c$coefficients)[names(m6c$coefficients) == "lotteryvariance.FDP"] <- "Government Lottery Variance"
  names(m6c$coefficients)[names(m6c$coefficients) == "lotterymean.FDP"] <- "Government Lottery Mean"
  names(m6d$coefficients)[names(m6d$coefficients) == "lotteryvariance.GRUENE"] <- "Government Lottery Variance"
  names(m6d$coefficients)[names(m6d$coefficients) == "lotterymean.GRUENE"] <- "Government Lottery Mean"
  names(m6e$coefficients)[names(m6e$coefficients) == "lotteryvariance.LINKE"] <- "Government Lottery Variance"
  names(m6e$coefficients)[names(m6e$coefficients) == "lotterymean.LINKE"] <- "Government Lottery Mean"
  
  
  # Wave 7, 2017
  names(m7a$coefficients)[names(m7a$coefficients) == "lotteryvariance.UNION"] <- "Government Lottery Variance"
  names(m7a$coefficients)[names(m7a$coefficients) == "lotterymean.UNION"] <- "Government Lottery Mean"
  names(m7b$coefficients)[names(m7b$coefficients) == "lotteryvariance.SPD"] <- "Government Lottery Variance"
  names(m7b$coefficients)[names(m7b$coefficients) == "lotterymean.SPD"] <- "Government Lottery Mean"
  names(m7c$coefficients)[names(m7c$coefficients) == "lotteryvariance.FDP"] <- "Government Lottery Variance"
  names(m7c$coefficients)[names(m7c$coefficients) == "lotterymean.FDP"] <- "Government Lottery Mean"
  names(m7d$coefficients)[names(m7d$coefficients) == "lotteryvariance.GRUENE"] <- "Government Lottery Variance"
  names(m7d$coefficients)[names(m7d$coefficients) == "lotterymean.GRUENE"] <- "Government Lottery Mean"
  names(m7e$coefficients)[names(m7e$coefficients) == "lotteryvariance.LINKE"] <- "Government Lottery Variance"
  names(m7e$coefficients)[names(m7e$coefficients) == "lotterymean.LINKE"] <- "Government Lottery Mean"
  
  
  # Stargazer output
  if(!robustnesscheck_pid){
  stargazer(m7a, m7b, m7c, m7d, title="Linear regressions of vote choice on perceived government lottery variance and mean (GLES Panel 2016-2021, Wave 7 (2017)).", 
            dep.var.labels=c("Union", "SPD", "FDP", "Greens", "Left"), no.space=T,
            omit.stat=c("LL","ser","f"),
            se = lapply(list(m7a, m7b, m7c, m7d), se_robust),
            p = lapply(list(m7a, m7b, m7c, m7d), p_robust),
            out = "TableSM12.tex"
            )
  }